package javaWeb;

import Bean.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class PageServlet extends BasicServlet {
    protected void LastPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Page page = (Page) session.getAttribute("FurnPage");
        int maxSize = page.getMaxSize();
        page.setBeginIndex(maxSize);
        session.setAttribute("Page",page);
        req.setAttribute("isFirst","no");
        String pageType = req.getParameter("PageType");
        req.getRequestDispatcher("/javaWeb/"+pageType).forward(req,resp);
    }
    protected void FirstPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        Page page = (Page) session.getAttribute("FurnPage");
        page.setBeginIndex(1);
        session.setAttribute("Page",page);
        req.setAttribute("isFirst","no");
        String pageType = req.getParameter("PageType");
        req.getRequestDispatcher("/javaWeb/"+pageType).forward(req,resp);
    }
    protected void upPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        Page page = (Page) session.getAttribute("FurnPage");
        if (page.getBeginIndex() == 1){
            page.setBeginIndex(1);
        }else {
            page.setBeginIndex(page.getBeginIndex()-1);
        }
        session.setAttribute("Page",page);
        req.setAttribute("isFirst","no");
        String pageType = req.getParameter("PageType");
        req.getRequestDispatcher("/javaWeb/"+pageType).forward(req,resp);
    }
    protected void downPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        Page page = (Page) session.getAttribute("FurnPage");
        if (page.getBeginIndex() == page.getMaxSize()){
            page.setBeginIndex(page.getMaxSize());
        }else {
            page.setBeginIndex(page.getBeginIndex()+1);
        }
        session.setAttribute("Page",page);
        req.setAttribute("isFirst","no");
        String pageType = req.getParameter("PageType");
        req.getRequestDispatcher("/javaWeb/"+pageType).forward(req,resp);
    }

    protected void WhichPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        HttpSession session = req.getSession();
        String index = req.getParameter("index");
        int i  = 0;
        try {
            i = Integer.parseInt(index);
        } catch (NumberFormatException e) {
            throw new RuntimeException(e);
        } finally {
        }
        Page page = (Page) session.getAttribute("FurnPage");
        page.setBeginIndex(i);
        session.setAttribute("Page",page);
        req.setAttribute("isFirst","no");
        String pageType = req.getParameter("PageType");
        req.getRequestDispatcher("/javaWeb/"+pageType).forward(req,resp);
    }


}
